// pages/index/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    selectedMoods: ['放松'],
    hasDrawnToday: false,
    bonusDrawCount: 0
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadUserData();
  },

  async loadUserData() {
    try {
      wx.showLoading({ title: '加载中...' });
      const app = getApp();
      
      // 刷新全局数据
      await app.refreshData();
      
      // 更新页面数据
      this.setData({
        selectedMoods: app.globalData.selectedMoods || ['放松'],
        hasDrawnToday: app.globalData.hasDrawnToday || false,
        bonusDrawCount: app.globalData.bonusDrawCount || 0
      });
    } catch (error) {
      console.error('加载用户数据失败:', error);
      
      // 即使失败也要显示基本数据
      const app = getApp();
      this.setData({
        selectedMoods: app.globalData.selectedMoods || ['放松'],
        hasDrawnToday: app.globalData.hasDrawnToday || false,
        bonusDrawCount: app.globalData.bonusDrawCount || 0
      });
    } finally {
      wx.hideLoading();
    }
  },

  changeMood: function() {
    wx.navigateTo({
      url: '../mood-select/mood-select'
    });
  },

  drawBlindBox: function() {
    const app = getApp();
    
    // 如果有额外抽奖次数，使用它
    if (app.globalData.bonusDrawCount > 0) {
      wx.navigateTo({
        url: '../draw/draw?type=bonus'
      });
    } else if (!app.globalData.hasDrawnToday) {
      wx.navigateTo({
        url: '../draw/draw?type=normal'
      });
    } else {
      // 已经抽过了，去看结果
      wx.navigateTo({
        url: '../history/history'
      });
    }
  },

  goToSubmit: function() {
    wx.navigateTo({
      url: '../submit/submit'
    });
  },

  goToHistory: function() {
    wx.switchTab({
      url: '../history/history'
    });
  },

  goToProfile: function() {
    wx.switchTab({
      url: '../profile/profile'
    });
  }
})